/*
 * Copyright 2011 Rolf Aden
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

package com.ec;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Mapper;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.tools.mapreduce.DatastoreMutationPool;
import com.google.appengine.tools.mapreduce.contrib.DatastoreMapper;

/**
 * Mapper to delete all entities of a kind.
 * 
 * @author Rolf Aden
 */
public class DeleteEntityKindMapper extends DatastoreMapper {
	
	private static final Logger LOG = Logger.getLogger(DeleteEntityKindMapper.class.getName());
	
	private DatastoreMutationPool mutationPool;

	@Override
	public void taskSetup(Mapper<Key,Entity,NullWritable,NullWritable>.Context context) throws IOException ,InterruptedException {
		this.mutationPool = this.getAppEngineContext(context).getMutationPool();
	};
	
	@Override
	public void map(Key key, Entity entity, Mapper<Key,Entity,NullWritable,NullWritable>.Context context) throws IOException, InterruptedException 
	{
		if (LOG.isLoggable(Level.FINER)) {
			LOG.finer("deleting " + entity);
		}
		this.mutationPool.delete(key);	
	}
}
